rhino.loadDex('Bear_plugin.dex')//先加载插件
var Blu_keyboard = com.bear_plugin.BleHid
var Otg = com.bear_plugin.OTG

function OTG_isOn() {
    return Otg.OTG_isOn()
}

function OTG_init() {
    ret = Otg.initOtg(app.getContext())
    Otg.setXY(screen.getScreenWidth(), screen.getScreenHeight())
    if (Otg.OTG_isOn()) {
        Otg.setXY(screen.getScreenWidth(), screen.getScreenHeight())
        print.log("OTG初始化成功")
        return true;
    }
    else {
        print.log("OTG未连接")
        return false;
    }
}

function Blu_keyboard_connect() {
    //初始化
    var bleDevice = Blu_keyboard.getDevice(app.getContext());
    if (!bleDevice) {
        print.err("蓝牙未打开!")
        return false
    }
    //开始链接设备
    if (!Blu_keyboard.connect(bleDevice, false)) {
        print.err("蓝牙未配对!")
        return false
    }
    for (i = 1; i < 100; i++) {
        var constate = Blu_keyboard.getConnectState();
        if (constate) {
            print.debug("链接设备成功!!")
            return true
        } else {
            printl("连接蓝牙中:", constate)
        }
        if (!constate && i == 99) {
            print.err("没有成功链接到蓝牙设备!")
            return false
        }
        sleep.millisecond(1000)
    }

    return false
}

function Blu_keyboard_close() {
    if (Blu_keyboard.BLEGatt != null) {
        try {
            print.debug("完全关闭连接")
            Blu_keyboard.BLEGatt.disconnect();
            Blu_keyboard.BLEGatt.close();
            Blu_keyboard.disconnect()
        } catch (e) {

        }
    }
}

function Blu_keyboard_init() {
    if (!Blu_keyboard.getConnectState()) {
        if (Blu_keyboard_connect()) {
            printl("蓝牙名:", Blu_keyboard.getName())
            //设置分辨率
            Blu_keyboard.ready = true;
		    for(var i=0;i<2;i++){
		            w= screen.getScreenWidth();
		            h = screen.getScreenHeight();
		            printl("屏幕h w", w,h)
		            Blu_keyboard.setXY(w, h)
		            
		    }
        } else {
            print.err("----->蓝牙初始化失败")
            //stopAll()
        }
    }
    try { Blu_keyboard.setSendMode(false) } catch (e) { }//需要最新版apk插件
    var errCount = 0
    
    return true;
}

//全随机仿真上下划
/*
HidSwipeEx(getRnd(450, 550), getRnd(1700, 1900), getRnd(450, 550), getRnd(300, 500), getRnd(100, 150))//上划
sleep.millisecond(getRnd(1300, 2000));
HidSwipeEx(getRnd(450, 550), getRnd(300, 500), getRnd(450, 550),  getRnd(1700, 1900), getRnd(100, 150))//下滑
*/

//下面是一些基础例子!!!!!!!
//HidSwipe(起点x, 起点y, 终点x, 终点y, 滑动时间(可选),按下时间(可选),抬起时间(可选))
//简单用法,1000毫秒慢速滑动
//HidSwipe(500, 20, 700, 1900,1000)
//随机滑动例子(根据上面简单改装了一下,仅供参考)
//蓝牙滑动函数封装例子  otg不需要

function HidSwipeEx(sx, sy, ex, ey, leaveHeightLength) {
    var timeMin = 500;
    var timeMax = 1000;
    leaveHeightLength = leaveHeightLength || 100;
    var bezierCreate = function (x1, y1, x2, y2, x3, y3, x4, y4, numberOfPoints) {
        var h = 100;
        var cp = [{ x: x1, y: y1 + h }, { x: x2, y: y2 + h }, { x: x3, y: y3 + h }, { x: x4, y: y4 + h }];
        numberOfPoints = numberOfPoints || 100;
        printl("控制点:", numberOfPoints)
        var curve = [];
        var dt = 1.0 / (numberOfPoints - 1);
        for (var i = 0; i < numberOfPoints; i++) {
            var ax, bx, cx;
            var ay, by, cy;
            var tSquared, tCubed;
            var result_x, result_y;
            cx = 3.0 * (cp[1].x - cp[0].x);
            bx = 3.0 * (cp[2].x - cp[1].x) - cx;
            ax = cp[3].x - cp[0].x - cx - bx;
            cy = 3.0 * (cp[1].y - cp[0].y);
            by = 3.0 * (cp[2].y - cp[1].y) - cy;
            ay = cp[3].y - cp[0].y - cy - by;
            var t = dt * i
            tSquared = t * t;
            tCubed = tSquared * t;
            result_x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
            result_y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;

            curve[i] = {
                x: result_x,
                y: result_y
            };

        }
        var array = [];
        for (var i = 0; i < curve.length; i++) {
            try {
                var j = (i < 100) ? i : (199 - i);
                xx = parseInt(curve[j].x)
                yy = parseInt(Math.abs(100 - curve[j].y))
            } catch (e) {

                break
            }
            //printl(curve[j],[xx, yy])
            array.push([xx, yy])
        }
        return array

    }
    var x2, x3, y2, y3, mx, my;
    if (Math.abs(ex - sx) > Math.abs(ey - sy)) {
        my = (sy + ey) / 2;
        y2 = my + rand.randNumber(0, leaveHeightLength);
        y3 = my - rand.randNumber(0, leaveHeightLength);
        x2 = sx + (ex - sx) / 3 + rand.randNumber(0, (ex - sx) / 3);
        x3 = sx + 2 * (ex - sx) / 3 + rand.randNumber(0, (ex - sx) / 3);
    } else {
        mx = (sx + ex) / 2;
        x2 = mx + rand.randNumber(0, leaveHeightLength);
        x3 = mx - rand.randNumber(0, leaveHeightLength);
        y2 = sy + (ey - sy) / 3 + rand.randNumber(0, (ey - sy) / 3);
        y3 = sy + 2 * (ey - sy) / 3 + rand.randNumber(0, (ey - sy) / 3);
    }

    var track = bezierCreate(sx, sy, x2, y2, x3, y3, ex, ey, 30);
    var time = [0, rand.randNumber(timeMin, timeMax)];

    Blu_keyboard.touchDown(track[0][0], track[0][1]);
    sleep.millisecond(rand.randNumber(100, 150));
    for (var i = 1; i < track.length; i++) {
        Blu_keyboard.touchMove(track[i][0], track[i][1]);
        //printl(track[i][0], track[i][1])
        sleep.millisecond(getRnd(5, 20));
    }
    sleep.millisecond(rand.randNumber(80, 150));
    Blu_keyboard.touchUp();
    sleep.millisecond(rand.randNumber(30, 50));
}
